package com.android.mios.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.android.mios.util.Settings;
import com.android.mios.util.Utils;

public class Engine implements Serializable {

	public static final String CONNECTION_MODE_NONE = "N";
	public static final String CONNECTION_MODE_LOCAL = "L";
	public static final String CONNECTION_MODE_REMOTE = "R";

	private static final long serialVersionUID = 2195525756416410403L;
	public static final int CATEGORY_CAMERA = 2;
	public static final int CATEGORY_TV = 3;

	private int  serialNumber;
	private int loadTime;
	private int dataVersion;
	private int ir = 0;

	private String firmwareVersion;
	private String name;
	private String ipAddress;
	private List<String> users;
	private String activeServer;
	private List<ForwardServer> forwardServers = new ArrayList<ForwardServer>();
	private boolean accessories;
	private int category;
	private int subCategory;
	private List<Integer> units;
	private String streaming;
	private String url;
	private String model;
	private int state = State.DISCONNECTED;
	private int remote;
	private String connectionMode;
	// private String pollingUrl = "";
	private Map<String, String> settings = new HashMap<String, String>();

	public static List<Engine> findMiosEngines(Map<Integer, Engine> allEngines) {
		List<Engine> miosEngines = new ArrayList<Engine>();

		for (Engine engine : allEngines.values()) {
			if (!engine.isAccessories()) {
				miosEngines.add(engine);
			}
		}
		return miosEngines;
	}

	public static List<Engine> findAccessoriesByCategory(
			Map<Integer, Engine> allEngines, int category) {
		List<Engine> accessories = new ArrayList<Engine>();
		for (Engine engine : allEngines.values()) {
			if (engine.isAccessories() && engine.category == category) {
				accessories.add(engine);
			}
		}
		return accessories;
	}

	public String getPollingUrl() {
		String url = "";
		if (ipAddress.length() < 7) {
			url += "https://" + getActiveServer() + "/"
					+ Settings.getInstance().getCurrentUsername() + "/"
					+ Settings.getInstance().getPassword() + "/"
					+ getSerialNumber();
		} else {
			url += "http://" + ipAddress + ":3480";
		}
		return url;
	}

	public int  getSerialNumber() {
		return serialNumber;
	}

	public String getModel() {
		return model;
	}

	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}

	public void setModel(String m) {
		this.model = m;
	}

	public String getFirmwareVersion() {
		return firmwareVersion;
	}

	public void setFirmwareVersion(String firmwareVersion) {
		this.firmwareVersion = firmwareVersion;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if ("null".equalsIgnoreCase(name)) {
			name = null;
		}
		this.name = name;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public List<String> getUsers() {
		return users;
	}

	public void setUsers(List<String> users) {
		this.users = users;
	}

	public String getActiveServer() {
		return activeServer;
	}

	public void setActiveServer(String activeServer) {
		this.activeServer = activeServer;
	}

	public List<ForwardServer> getForwardServers() {
		return forwardServers;
	}

	public void setForwardServers(List<ForwardServer> forwardServers) {
		this.forwardServers = forwardServers;
	}

	public static class ForwardServer {
		private String hostName;
		private boolean primary;

		public String getHostName() {
			return hostName;
		}

		public void setHostName(String hostName) {
			this.hostName = hostName;
		}

		public boolean isPrimary() {
			return primary;
		}

		public void setPrimary(boolean primary) {
			this.primary = primary;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((hostName == null) ? 0 : hostName.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ForwardServer other = (ForwardServer) obj;
			if (hostName == null) {
				if (other.hostName != null)
					return false;
			} else if (!hostName.equals(other.hostName))
				return false;
			return true;
		}
	}

	public boolean isAccessories() {
		return accessories;
	}

	public void setAccessories(boolean accessories) {
		this.accessories = accessories;
	}

	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public int getSubCategory() {
		return subCategory;
	}

	public void setSubCategory(int subCategory) {
		this.subCategory = subCategory;
	}

	public List<Integer> getUnits() {
		return units;
	}

	public void setUnits(List<Integer> units) {
		this.units = units;
	}

	public String getStreaming() {
		return streaming;
	}

	public void setStreaming(String streaming) {
		this.streaming = streaming;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<String> getAllServerAddresses() {
		List<String> addresses = new ArrayList<String>();
		if (ipAddress != null) {
			addresses.add(ipAddress + ":3480");
		}
		if (activeServer != null) {
			addresses.add(activeServer);
		}
		if (forwardServers != null) {
			for (ForwardServer forwardServer : forwardServers) {
				String address = forwardServer.hostName;
				if (!addresses.contains(address)) {
					addresses.add(address);
				}
			}
		}
		return addresses;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public boolean canConnectLocally() {

		return !Utils.isEmpty(ipAddress);// &&
											// Settings.getInstance().isLastWifi();
	}

	@Override
	public String toString() {
		if (name == null) {
			return "No name";
		} else {
			return name;
		}
	}

	public int getRemote() {
		return remote;
	}

	public void setRemote(int remote) {
		this.remote = remote;
	}

	public String getConnectionMode() {
		return connectionMode;
	}

	public void setConnectionMode(String connectionMode) {
		this.connectionMode = connectionMode;
	}

	public Map<String, String> getSettings() {
		return settings;
	}

	public void setSettings(Map<String, String> settings) {
		this.settings = settings;
	}
	
	public int getLoadTime() {
		return loadTime;
	}

	public void setLoadTime(int loadTime) {
		this.loadTime = loadTime;
	}

	public int getDataVersion() {
		return dataVersion;
	}

	public void setDataVersion(int dataVersion) {
		this.dataVersion = dataVersion;
	}

	public int getIr() {
		return ir;
	}

	public void setIr(int ir) {
		this.ir = ir;
	}
	
	public void clearData(){
		dataVersion=0;
		loadTime=0;
		ir=0;
	}
}
